package com.example.controller.sys;

import com.example.pojo.RoleMenu;
import com.example.service.IRoleMenuService;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.List;

/**
 * <p>
 * 角色菜单表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/sys/role-menu")
@Api(tags = "角色菜单管理",value = "")
public class RoleMenuController {

    @Autowired
    private IRoleMenuService roleMenuService;

    @ApiOperation(value = "获取所有角色菜单")
    @GetMapping("")
    public PageInfo<RoleMenu> getRoleMenus(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<RoleMenu> list = roleMenuService.list();
        return new PageInfo<>(list);
    }

    @ApiOperation(value = "添加角色菜单")
    @PostMapping("")
    public ResultVo addRoleMenu(@RequestBody RoleMenu roleMenu){
        if(roleMenuService.save(roleMenu)){
            return ResultVo.success("添加成功。");
        }
        return ResultVo.error("添加失败!");
    }

    @ApiOperation(value = "修改角色菜单")
    @PutMapping("/update")
    public ResultVo updateRoleMenu(@RequestBody RoleMenu roleMenu){
        return roleMenuService.updateRoleMenu(roleMenu);
    }

    @ApiOperation(value = "")
    @DeleteMapping("/{id}")
    public ResultVo deleteRoleMenu(@PathVariable("id") Integer id){
        if(roleMenuService.removeById(id)){
            return ResultVo.success("删除成功。");
        }
        return ResultVo.error("删除失败!");
    }
}
